﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;


using FL.Common;

namespace FL.Bussiness.Site
{
    public class BBaseSite :BaseBussnies
    {

        #region 公用方法
        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        public virtual IList<T> GetEntitysSplitPage<T>(string condition = "")
        {
            string SqlComm = string.Empty;
            try
            {
                T entity = Activator.CreateInstance<T>();
                this.entity = entity as FL.DBModule.BaseEntity;

                this.Order = (this.Order == "" ? this.entity.getKey() : this.Order);

                string order = string.IsNullOrEmpty(this.Order) ? "" : "( Order by " + this.Order + "  ";
                if (!order.Contains(",") && !order.ToLower().Contains("desc") && !order.ToLower().Contains("asc"))
                {
                    order = string.IsNullOrEmpty(order) ? "" : order + (this.Desc == "Yes" ? " Desc) " : " Asc) ");
                }
                if (order == "")
                {
                    order = "(order by id desc) ";
                }
                if (!order.Contains(")"))
                {
                    order = order + ") ";
                }

                SqlComm = "SELECT TOP " + PageSize.ToString() + " * FROM  (   SELECT ROW_NUMBER() OVER" + order + " AS RowNumber,* FROM " + this.entity.GetTableName() + ") A WHERE " + (condition == "" ? "" : condition + " and ") + " RowNumber > " + (PageIndex - 1) * PageSize + " order by rowNumber"; ;
                DataTable dt = this.GetDataBySqlComm(SqlComm);
                IList<T> list = new List<T>();
                foreach (DataRow row in dt.Rows)
                {
                    list.Add(DataRowToTEntity<T>(row));
                }
                return list;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        /// 通过查询结果，返回单个实体对象;
        /// </summary>
        /// <param name="row">数据行</param>
        /// <returns>实体对象</returns>
        public T DataRowToTEntity<T>(DataRow row)
        {
            T TEntity = Activator.CreateInstance<T>();

            Type t = TEntity.GetType();
            PropertyInfo[] Mypropertyinfos = t.GetProperties();


            foreach (PropertyInfo pi in Mypropertyinfos)
            {
                if (pi.Name == "RowNumber" || pi.Name == "SubList")
                {
                    continue;
                }
                try
                {
                    Globals.SetPropertyValue(TEntity, pi.Name, row.ItemArray[row.Table.Columns[pi.Name].Ordinal].ToString());
                }
                catch (System.Exception e)
                {
                    //throw e;
                }
            }
            return TEntity;
        }
        #endregion
    }
}